{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-08-11 23:24:29,506 [INFO] runing in interactive session [<module>] (D:\\workspace\\hikyuu\\hikyuu\\__init__.py:109) [hikyuu::hku_info]\n",
      "2025-08-11 23:24:29,507 [INFO] running in jupyter [<module>] (D:\\workspace\\hikyuu\\hikyuu\\__init__.py:116) [hikyuu::hku_info]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "current plugin path: D:\\workspace\\hikyuu\\hikyuu\\plugin\n",
      "2025-08-11 23:24:29.570 [HKU-I] - Using MYSQL BaseInfoDriver (BaseInfoDriver.cpp:57)\n",
      "2025-08-11 23:24:29.599 [HKU-I] - 加载市场信息…… (StockManager.cpp:572)\n",
      "2025-08-11 23:24:29.606 [HKU-I] - 加载证券类型信息…… (StockManager.cpp:590)\n",
      "2025-08-11 23:24:29.612 [HKU-I] - 加载证券信息…… (StockManager.cpp:476)\n",
      "2025-08-11 23:24:29.744 [HKU-I] - 加载权息数据…… (StockManager.cpp:608)\n",
      "2025-08-11 23:24:30.755 [HKU-I] - 加载板块信息…… (StockManager.cpp:181)\n",
      "2025-08-11 23:24:31.845 [HKU-I] - 加载K线数据…… (StockManager.cpp:185)\n",
      "2025-08-11 23:24:31.845 [HKU-I] - 预加载 day K线数据至缓存 (最大数量: 100000)! (StockManager.cpp:229)\n",
      "2025-08-11 23:24:31.846 [HKU-I] - 2.26 秒数据加载完毕. (StockManager.cpp:191)\n",
      "2025-08-11 23:24:31.847 [HKU-I] - 启动行情接收. (SpotAgent.cpp:39)\n",
      "CPU times: total: 1.81 s\n",
      "Wall time: 3.67 s\n"
     ]
    }
   ],
   "source": [
    "#%matplotlib inline\n",
    "%time from hikyuu.interactive import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1 全局获取股票对象\n",
    "==========\n",
    "\n",
    "1.1 获取股票对象\n",
    "-----------------\n",
    "\n",
    "通过全局管理对象 sm，或使用函数 get_stock。股票标识格式“市场标识+股票代码”，市场标识：沪市sh，深市sz。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Stock(SH, 000001, 上证指数, 指数, 1, 1990-12-20 00:00:00, +infinity)\n"
     ]
    }
   ],
   "source": [
    "#s = getStock('sh000001')\n",
    "s = sm['sh000001']\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.2 遍历所有股票\n",
    "-----------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "全部数量: 8471\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "8471"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = 0\n",
    "#遍历所有股票\n",
    "for s in sm:\n",
    "    i += 1\n",
    "    #print(s)\n",
    "print(\"全部数量:\", i)\n",
    "\n",
    "len(sm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2 通过板块（Block）遍历股票对象\n",
    "================\n",
    "\n",
    "通过 sm.get_stock(\"板块分类\", \"板块名称\") 获取相应板块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "300\n"
     ]
    }
   ],
   "source": [
    "blk = sm.get_block(\"指数板块\", \"沪深300\")\n",
    "print(len(blk))\n",
    "# for s in blk:\n",
    "#     if s.valid:\n",
    "#         print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3 查看权息信息\n",
    "======="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Weight(1990-03-01 00:00:00, 0, 1, 3.56, 0, 0, 0, 0, 0)\n",
      "Weight(1991-04-03 00:00:00, 0, 0, 0, 0, 0, 4850.02, 2650, 0)\n",
      "Weight(1991-05-02 00:00:00, 4, 0, 0, 3, 0, 8975.16, 3949.07, 0)\n",
      "Weight(1991-08-17 00:00:00, 10, 0, 0, 0, 0, 8975.16, 3949.07, 0)\n",
      "Weight(1992-03-23 00:00:00, 5, 0, 0, 2, 0, 8975.16, 3949.07, 0)\n",
      "Weight(1993-05-24 00:00:00, 8.5, 1, 16, 3, 0, 26941.8, 18813, 0)\n",
      "Weight(1993-06-30 00:00:00, 0, 0, 0, 0, 0, 26125.4, 18004.3, 0)\n",
      "Weight(1994-07-09 00:00:00, 0, 0, 0, 0, 0, 28737.9, 19804.8, 0)\n",
      "Weight(1994-07-11 00:00:00, 5, 1, 5, 5, 0, 28737.9, 19804.8, 0)\n",
      "Weight(1994-07-14 00:00:00, 0, 0, 0, 0, 0, 43106.9, 29707.2, 0)\n",
      "Weight(1995-09-25 00:00:00, 2, 0, 0, 3, 0, 43106.9, 29707.2, 0)\n",
      "Weight(1995-09-27 00:00:00, 0, 0, 0, 0, 0, 51728.2, 35648.6, 0)\n",
      "Weight(1996-05-27 00:00:00, 10, 0, 0, 0, 0, 51728.2, 35648.6, 0)\n",
      "Weight(1996-05-29 00:00:00, 0, 0, 0, 0, 0, 103456, 71442.3, 0)\n",
      "Weight(1997-08-25 00:00:00, 5, 0, 0, 2, 0, 103456, 71442.3, 0)\n",
      "Weight(1997-08-27 00:00:00, 0, 0, 0, 0, 0, 155185, 107163, 0)\n",
      "Weight(1999-10-18 00:00:00, 0, 0, 0, 6, 0, 155185, 107163, 0)\n",
      "Weight(2000-11-06 00:00:00, 0, 3, 8, 0, 0, 155185, 107163, 0)\n",
      "Weight(2000-12-08 00:00:00, 0, 0, 0, 0, 0, 194582, 139312, 0)\n",
      "Weight(2001-10-15 00:00:00, 0, 0, 0, 0, 0, 194582, 140936, 0)\n",
      "Weight(2002-07-23 00:00:00, 0, 0, 0, 1.5, 0, 194582, 140936, 0)\n",
      "Weight(2003-09-29 00:00:00, 0, 0, 0, 1.5, 0, 194582, 140936, 0)\n",
      "Weight(2007-06-18 00:00:00, 1, 0, 0, 0, 0, 194582, 140936, 0)\n",
      "Weight(2007-06-20 00:00:00, 0, 0, 0, 0, 0, 208676, 155019, 0)\n",
      "Weight(2007-12-28 00:00:00, 0, 0, 0, 0, 0, 229341, 170327, 0)\n",
      "Weight(2007-12-31 00:00:00, 0, 0, 0, 0, 0, 229341, 170326, 0)\n",
      "Weight(2008-01-21 00:00:00, 0, 0, 0, 0, 0, 229341, 175682, 0)\n",
      "Weight(2008-06-26 00:00:00, 0, 0, 0, 0, 0, 229341, 204652, 0)\n",
      "Weight(2008-06-27 00:00:00, 0, 0, 0, 0, 0, 238880, 214200, 0)\n",
      "Weight(2008-10-31 00:00:00, 3, 0, 0, 0.335, 0, 310543, 278461, 0)\n",
      "Weight(2009-06-22 00:00:00, 0, 0, 0, 0, 0, 310543, 292367, 0)\n",
      "Weight(2009-06-30 00:00:00, 0, 0, 0, 0, 0, 310543, 292376, 0)\n",
      "Weight(2009-10-15 00:00:00, 0, 0, 0, 0, 0, 310543, 292411, 0)\n",
      "Weight(2010-06-28 00:00:00, 0, 0, 0, 0, 0, 310543, 310537, 0)\n",
      "Weight(2010-06-29 00:00:00, 0, 0, 0, 0, 0, 348501, 310537, 0)\n",
      "Weight(2010-06-30 00:00:00, 0, 0, 0, 0, 0, 348501, 310537, 0)\n",
      "Weight(2010-12-31 00:00:00, 0, 0, 0, 0, 0, 348501, 310536, 0)\n",
      "Weight(2011-06-30 00:00:00, 0, 0, 0, 0, 0, 348501, 310536, 0)\n",
      "Weight(2011-08-05 00:00:00, 0, 0, 0, 0, 0, 512335, 310536, 0)\n",
      "Weight(2012-10-19 00:00:00, 0, 0, 0, 1, 0, 512335, 310536, 0)\n",
      "Weight(2013-06-20 00:00:00, 6, 0, 0, 1.7, 0, 819736, 496857, 0)\n",
      "Weight(2013-11-12 00:00:00, 0, 0, 0, 0, 0, 819736, 557590, 0)\n",
      "Weight(2014-01-09 00:00:00, 0, 0, 0, 0, 0, 952075, 557590, 0)\n",
      "Weight(2014-06-12 00:00:00, 2, 0, 0, 1.6, 0, 1.14249e+06, 669106, 0)\n",
      "Weight(2014-09-01 00:00:00, 0, 0, 0, 0, 0, 1.14249e+06, 983671, 0)\n",
      "Weight(2015-04-13 00:00:00, 2, 0, 0, 1.74, 0, 1.37099e+06, 1.18041e+06, 0)\n",
      "Weight(2015-05-21 00:00:00, 0, 0, 0, 0, 0, 1.43087e+06, 1.18041e+06, 0)\n",
      "Weight(2016-05-23 00:00:00, 0, 0, 0, 0, 0, 1.43087e+06, 1.21926e+06, 0)\n",
      "Weight(2016-06-16 00:00:00, 2, 0, 0, 1.53, 0, 1.71704e+06, 1.46312e+06, 0)\n",
      "Weight(2017-01-09 00:00:00, 0, 0, 0, 0, 0, 1.71704e+06, 1.6918e+06, 0)\n",
      "Weight(2017-07-21 00:00:00, 0, 0, 0, 1.58, 0, 1.71704e+06, 1.6918e+06, 0)\n",
      "Weight(2017-12-31 00:00:00, 0, 0, 0, 0, 0, 1.71704e+06, 1.6918e+06, 0)\n",
      "Weight(2018-05-21 00:00:00, 0, 0, 0, 0, 0, 1.71704e+06, 1.71702e+06, 0)\n",
      "Weight(2018-07-12 00:00:00, 0, 0, 0, 1.36, 0, 1.71704e+06, 1.71702e+06, 0)\n",
      "Weight(2019-06-26 00:00:00, 0, 0, 0, 1.45, 0, 1.71704e+06, 1.71702e+06, 0)\n",
      "Weight(2019-06-30 00:00:00, 0, 0, 0, 0, 0, 1.71704e+06, 1.71702e+06, 0)\n",
      "Weight(2019-09-18 00:00:00, 0, 0, 0, 0, 0, 1.94059e+06, 1.94058e+06, 0)\n",
      "Weight(2020-05-28 00:00:00, 0, 0, 0, 2.18, 0, 1.94059e+06, 1.94058e+06, 0)\n",
      "Weight(2020-12-31 00:00:00, 0, 0, 0, 0, 0, 1.94059e+06, 1.94058e+06, 0)\n",
      "Weight(2021-05-14 00:00:00, 0, 0, 0, 1.8, 0, 1.94059e+06, 1.94058e+06, 0)\n",
      "Weight(2021-12-31 00:00:00, 0, 0, 0, 0, 0, 1.94059e+06, 1.94055e+06, 0)\n",
      "Weight(2022-06-30 00:00:00, 0, 0, 0, 0, 0, 1.94059e+06, 1.94055e+06, 0)\n",
      "Weight(2022-07-22 00:00:00, 0, 0, 0, 2.28, 0, 1.94059e+06, 1.94055e+06, 0)\n",
      "Weight(2023-06-14 00:00:00, 0, 0, 0, 2.85, 0, 1.94059e+06, 1.94055e+06, 0)\n",
      "Weight(2024-06-14 00:00:00, 0, 0, 0, 7.19, 0, 1.94059e+06, 1.94055e+06, 0)\n",
      "Weight(2024-06-30 00:00:00, 0, 0, 0, 0, 0, 1.94059e+06, 1.94056e+06, 0)\n",
      "Weight(2024-10-10 00:00:00, 0, 0, 0, 2.46, 0, 1.94059e+06, 1.94056e+06, 0)\n",
      "Weight(2024-12-31 00:00:00, 0, 0, 0, 0, 0, 1.94059e+06, 1.94056e+06, 0)\n",
      "Weight(2025-06-12 00:00:00, 0, 0, 0, 3.62, 0, 1.94059e+06, 1.94056e+06, 0)\n"
     ]
    }
   ],
   "source": [
    "ws = sm['sz000001'].get_weight()\n",
    "for w in ws:\n",
    "    print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>countAsGift</th>\n",
       "      <th>countForSell</th>\n",
       "      <th>priceForSell</th>\n",
       "      <th>bonus</th>\n",
       "      <th>countOfIncreasement</th>\n",
       "      <th>totalCount</th>\n",
       "      <th>freeCount</th>\n",
       "      <th>suogu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1990-03-01</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.56</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1991-04-03</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.850017e+03</td>\n",
       "      <td>2.650000e+03</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1991-05-02</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.975164e+03</td>\n",
       "      <td>3.949072e+03</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1991-08-17</td>\n",
       "      <td>10.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.975164e+03</td>\n",
       "      <td>3.949072e+03</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1992-03-23</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.975164e+03</td>\n",
       "      <td>3.949072e+03</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>2024-06-14</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>7.19</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.940592e+06</td>\n",
       "      <td>1.940555e+06</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>2024-06-30</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.940592e+06</td>\n",
       "      <td>1.940562e+06</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>2024-10-10</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2.46</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.940592e+06</td>\n",
       "      <td>1.940562e+06</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67</th>\n",
       "      <td>2024-12-31</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.940592e+06</td>\n",
       "      <td>1.940557e+06</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>68</th>\n",
       "      <td>2025-06-12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>3.62</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.940592e+06</td>\n",
       "      <td>1.940557e+06</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>69 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     datetime  countAsGift  countForSell  priceForSell  bonus  \\\n",
       "0  1990-03-01          0.0           1.0          3.56   0.00   \n",
       "1  1991-04-03          0.0           0.0          0.00   0.00   \n",
       "2  1991-05-02          4.0           0.0          0.00   3.00   \n",
       "3  1991-08-17         10.0           0.0          0.00   0.00   \n",
       "4  1992-03-23          5.0           0.0          0.00   2.00   \n",
       "..        ...          ...           ...           ...    ...   \n",
       "64 2024-06-14          0.0           0.0          0.00   7.19   \n",
       "65 2024-06-30          0.0           0.0          0.00   0.00   \n",
       "66 2024-10-10          0.0           0.0          0.00   2.46   \n",
       "67 2024-12-31          0.0           0.0          0.00   0.00   \n",
       "68 2025-06-12          0.0           0.0          0.00   3.62   \n",
       "\n",
       "    countOfIncreasement    totalCount     freeCount  suogu  \n",
       "0                   0.0  0.000000e+00  0.000000e+00    0.0  \n",
       "1                   0.0  4.850017e+03  2.650000e+03    0.0  \n",
       "2                   0.0  8.975164e+03  3.949072e+03    0.0  \n",
       "3                   0.0  8.975164e+03  3.949072e+03    0.0  \n",
       "4                   0.0  8.975164e+03  3.949072e+03    0.0  \n",
       "..                  ...           ...           ...    ...  \n",
       "64                  0.0  1.940592e+06  1.940555e+06    0.0  \n",
       "65                  0.0  1.940592e+06  1.940562e+06    0.0  \n",
       "66                  0.0  1.940592e+06  1.940562e+06    0.0  \n",
       "67                  0.0  1.940592e+06  1.940557e+06    0.0  \n",
       "68                  0.0  1.940592e+06  1.940557e+06    0.0  \n",
       "\n",
       "[69 rows x 9 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ws.to_df()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
